﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.ComponentModel;

namespace YFIN.Admin.Helper
{
    public class NavigateToAction : Behavior<Control>
    {
        string curState;
        public static event EventHandler<ItemClickedEventArgs> Clicked;
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.MouseLeftButtonUp += new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonUp);
            AssociatedObject.Loaded += new RoutedEventHandler(AssociatedObject_Loaded);
            NavigateToAction.Clicked += new EventHandler<ItemClickedEventArgs>(TMNavigateToAction_Clicked);
            curState = InactiveStateName;
        }
        void TMNavigateToAction_Clicked(object sender, ItemClickedEventArgs e)
        {
            if (e.itemName != AssociatedObject.Name && (curState == ActiveStateName))
            {
                VisualStateManager.GoToState(AssociatedObject, InactiveStateName, false);
                AssociatedObject.IsEnabled = true;
            }
        }
        void Menu_Loaded(object sender, EventArgs e)
        {
            if (System.Windows.Browser.HtmlPage.Document.DocumentUri.OriginalString.Contains(Url.OriginalString) && (Url.OriginalString != ""))
            {
                VisualStateManager.GoToState(AssociatedObject, ActiveStateName, false);
                if (Url.ToString() == "#") AssociatedObject.IsEnabled = true;
                else AssociatedObject.IsEnabled = false;
                curState = ActiveStateName;
            }
        }
        void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
        {
            var mainPage = Application.Current.RootVisual as FrameworkElement;
            AddStateCompletedEventHandler((mainPage.FindName(RootElement) as FrameworkElement), RootElementStateName, Menu_Loaded);
        }
        public Storyboard GetStoryboardForState(FrameworkElement control, string stateName)
        {
            Storyboard stateStoryboard = null;
            var root = control.FindName("LayoutRoot") as FrameworkElement;
            var groups = VisualStateManager.GetVisualStateGroups(root);
            foreach (VisualStateGroup group in groups)
            {
                foreach (VisualState state in group.States)
                {
                    if (state.Name == stateName)
                    {
                        stateStoryboard = state.Storyboard;
                    }
                }
            }
            return stateStoryboard;
        }
        public void AddStateCompletedEventHandler(FrameworkElement control, string stateName, EventHandler stateChanged)
        {
            Storyboard stateStoryboard = GetStoryboardForState(control, stateName);
            if (null != stateStoryboard && null != stateChanged)
            {
                stateStoryboard.Completed += (s, e) => stateChanged(s, new EventArgs());
            }
        }
        void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            VisualStateManager.GoToState(AssociatedObject, ActiveStateName, false);
            curState = ActiveStateName;
            if (Url.ToString() != "#")
            {
                System.Windows.Browser.HtmlPage.Window.Navigate(Url);
            }
            AssociatedObject.IsEnabled = false;
            NavigateToAction.Clicked.Invoke(sender, new ItemClickedEventArgs(AssociatedObject.Name));
        }
        [Category("Common Properties"), Description("Gets or sets Url to navigate")]
        public Uri Url
        {
            get { return (Uri)GetValue(UriProperty); }
            set { SetValue(UriProperty, value); }
        }
        public static readonly DependencyProperty UriProperty =
            DependencyProperty.Register("Uri", typeof(Uri), typeof(NavigateToAction), new PropertyMetadata(new Uri("", UriKind.RelativeOrAbsolute)));
        [Category("Common Properties"), Description("Gets or sets the name of the state for the item when the page it is linked to is active")]
        public string ActiveStateName
        {
            get;
            set;
        }
        [Category("Common Properties"), Description("Gets or sets the name of the state for the item when the page it is linked to is inactive")]
        public string InactiveStateName
        {
            get;
            set;
        }
        [Category("Common Properties"), Description("Gets or sets the name of the state that the root element should go to before button is highlighted"), CustomPropertyValueEditor(CustomPropertyValueEditor.StateName)]
        public string RootElementStateName
        {
            get;
            set;
        }
        [Category("Common Properties"), Description("Gets or sets the name of the root element that contains the initial loading animation"), CustomPropertyValueEditor(CustomPropertyValueEditor.Element)]
        public string RootElement
        {
            get;
            set;
        }
        protected override void OnDetaching()
        {
            base.OnDetaching();
        }
    }
    public class ItemClickedEventArgs : EventArgs
    {
        public string itemName;
        public ItemClickedEventArgs(string item)
        {
            itemName = item;
        }
    }
}
